<?php

/***************************************************************\
* FILE:		sys/pages/add_item.inc
* SECTION:	pages > items
* FUNCTION:	add items
* USES:	
* INCLUDES:	/sys/itemfunctions.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/itemfunctions.inc');

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
	//check_access("ADD");
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	switch($action) {
		default:
			add_alert("unknown action");
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {

	global $PAGE, $filename;
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];

	if (!$THISDIR['show_items'])
		endpage("Hier kunnen geen items toegevoegd worden");

	if ($_POST['submitted']) {
		
		// check submitted content
		if (item_page_inc_check_content($_POST, $_FILES, item_page_inc_type_string($_POST['item_type_id']), 1)) {
		
			// if ok, insert into db
			
			// derive info stuff
			switch($_POST['item_type_id']) {
				case 1: // plain topic
				case 2: // article
				case 3: // link
					$info = substr($_POST['link'], 0, 50) . ((strlen($_POST['link']) > 50) ? "..." : "");
					break;
				case 4: // download
				case 5: // poll
				case 6: // news
					$info = strip_tags($_POST['body']);
					$info = substr($info, 0, 50) . ((strlen($info) > 50) ? "..." : "");
					break;
				case 8: // gallery
					if ($_POST['open_for_all'])
						$info = "publieke galerij";
					else
						if (ereg("[szx]$", $_SESSION['member']['nick']))
							$info = "{$_SESSION['member']['nick']}' galerij";
						else
							$info = "{$_SESSION['member']['nick']}s galerij";
					break;
				case 9: // ad
					include_once('sys/regionfunctions.inc');
					$info = region_format(region_get_region_tree($_POST['region']));
					if (ereg("^[0-9]+$", $_POST['game_id'])) {
						$sqlresult = db_do_query(db_query_select(array(array('name')), array('GAME'), "game_id = {$_POST['game_id']}"));
						if (db_num_rows($sqlresult) == 1) {
							$g = db_fetch_array($sqlresult);
							$info = "{$g['name']} - $info";
						}
					} else if (strlen($_POST['game_name']) > 0) 
						$info = "{$_POST['game_name']} - $info";
					break;
				case 10: // event
					include_once('sys/regionfunctions.inc');
					$date = $_POST['date_start_year'] . (($_POST['date_start_month'] > 9) ? "{$_POST['date_start_month']}" : "0{$_POST['date_start_month']}") . (($_POST['date_start_day'] > 9) ? "{$_POST['date_start_day']}" : "0{$_POST['date_start_day']}") . "000000";
					$info = date_format($date, 0, -1, 1) . " - ";
					$info .= region_format(region_get_region_tree($_POST['region'])); 
					break;
				case 11: // spellenfiche
					if ($_POST['game_editor_id'] == "NEW")
						$info = $_POST['editor_name'];
					else {
						$kv = get_keyed_values('game_editor_id', 'editor_name', 'GAME_EDITOR');
						$info = $kv[$_POST['game_editor_id']];
					}
					if ($_POST['game_id'] == "NEW")
						$title = $_POST['game_name'];
					else {
						$kv = get_keyed_values('game_id', 'name', 'GAME');
						$title = $kv[$_POST['game_id']];
					}
					break;
				default:
					$info = "";
					break;
			}
			$info = db_sqlize($info);
			
			$title = $title ? $title : $_POST['title'];
			
			// item insertion
			$sql = db_query_insert(array("item_type_id", "author", "editor", "title", "info", "hits", "creation", "edit", "num_posts"), array($_POST['item_type_id'], $_SESSION['user_id'], $_SESSION['user_id'], db_sqlize($title), $info, 0, "NOW(14)", "NOW(14)", ($_POST['item_type_id'] == 1 ? 1 : 0)), "ITEM");
			db_do_query($sql);
			
			// type-specific insertion
			$item_id = db_insert_id();
			switch($_POST['item_type_id']) {
				case 1: // plain topic
					$sql = db_query_insert(array('item_id', 'author', 'editor', 'title', 'creation', 'edit', 'postcount'), array($item_id, $_SESSION['user_id'], $_SESSION['user_id'], db_sqlize($_POST['title']), 'NOW(14)', 'NOW(14)', (intval($_SESSION['member']['postcount'])+1)), "POST");
					db_do_query($sql);
					$post_id = db_insert_id();
					$_SESSION['member']['postcount']++;
					$sql = db_query_update(array('postcount', 'member_since', 'last_visit'), array('postcount+1', 'member_since', 'last_visit'), "MEMBER", "user_id = {$_SESSION['user_id']}");
					db_do_query($sql);
					$sql = db_query_insert(array('post_id', 'body'), array($post_id, db_sqlize($_POST['body'])), "POST_BODY");
					break;
				case 2: // article
					$sql = db_query_insert(array('item_id', 'body'), array($item_id, db_sqlize($_POST['body'])), "ARTICLE");
					break;
				case 3: // link
					$sql = db_query_insert(array('item_id', 'link', 'body'), array($item_id, db_sqlize($_POST['link']), db_sqlize($_POST['body'])), "LINK");
					break;
				case 4: // download
					$sql = db_query_insert(array('item_id', 'body', 'filename'), array($item_id, db_sqlize($_POST['body']), db_sqlize($filename)), "DOWNLOAD");
					break;
				case 5: // poll
					for ($i = 0; $i < $_POST['num_answers']; $i++) {
						$sql = db_query_insert(array('item_id', 'answer'), array($item_id, db_sqlize($_POST["answer_$i"])), "POLL_ANSWER");
						db_do_query($sql);
					}
					$sql = db_query_insert(array('item_id', 'body'), array($item_id, db_sqlize($_POST['body'])), "POLL");	
					break;
				case 6: // news
					$sql = db_query_insert(array('item_id', 'body'), array($item_id, db_sqlize($_POST['body'])), "NEWS");
					break;
				case 8: // gallery
					$sql = db_query_insert(array('item_id', 'description', 'title', 'image', 'added', 'artist'), array($item_id, db_sqlize($_POST['art_description']), db_sqlize($_POST['art_title']), db_sqlize($filename), 'NOW(14)', $_SESSION['user_id']), "ART");
					db_do_query($sql);
					$sql = db_query_insert(array('item_id', 'body', 'open_for_all'), array($item_id, db_sqlize($_POST['body']), $_POST['open_for_all'] ? 1 : 0), "GALLERY");
					break;
				case 9: // ad
					if (ereg("^parent[0-9]+$", $_POST['region'])) { // new region
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST['region_name'])), "REGION"));
						$region_id = db_insert_id();
						db_do_query(db_query_insert(array('parent_region', 'sub_region'), array(substr($_POST['region'], 6), $region_id), "REGION_TREE"));
					} else
						$region_id = $_POST['region'];
					
					if ($_POST["game_id"] == "NEW") {
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST["game_name"])), "GAME"));
						$game_id = db_insert_id();
						db_do_query(db_query_insert(array('game_id', 'game_type_id'), array($game_id, $_POST["game_type"]), "GAME_OF_TYPE"));
					} else if ($_POST['game_id'] == "NONE")
						$game_id = "NULL";
					else
						$game_id = $_POST["game_id"];
						
					$sql = db_query_insert(array('item_id', 'game_id', 'region_id', 'body', 'seek'), array($item_id, $game_id, $region_id, db_sqlize($_POST['body']), ($_POST['seek'] == "GZ") ? 1 : 0), "AD");
					break;
				case 10: // event
					if (ereg("^parent[0-9]+$", $_POST['region'])) { // new region
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST['region_name'])), "REGION"));
						$region_id = db_insert_id();
						db_do_query(db_query_insert(array('parent_region', 'sub_region'), array(substr($_POST['region'], 6), $region_id), "REGION_TREE"));
					} else
						$region_id = $_POST['region'];
						
					$date = $_POST['date_start_year'] . (($_POST['date_start_month'] > 9) ? "{$_POST['date_start_month']}" : "0{$_POST['date_start_month']}") . (($_POST['date_start_day'] > 9) ? "{$_POST['date_start_day']}" : "0{$_POST['date_start_day']}") . "000000";	
					if ($_POST['date_end_day'])
						$enddate = $_POST['date_end_year'] . (($_POST['date_end_month'] > 9) ? "{$_POST['date_end_month']}" : "0{$_POST['date_end_month']}") . (($_POST['date_end_day'] > 9) ? "{$_POST['date_end_day']}" : "0{$_POST['date_end_day']}") . "000000";
					else
						$enddate = "NULL";
					$sql = db_query_insert(array('item_id', 'region_id', 'description', 'link', 'date_start', 'multiple_days', 'date_end'), array($item_id, $region_id, db_sqlize($_POST['description']), db_sqlize($_POST['link']), $date, $_POST['multiple_days'] ? 1 : 0, $enddate), "EVENT");
					break;
				case 11: // spellenfiche
					if ($_POST["game_id"] == "NEW") {
						db_do_query(db_query_insert(array('name'), array(db_sqlize($_POST["game_name"])), "GAME"));
						$game_id = db_insert_id();
						db_do_query(db_query_insert(array('game_id', 'game_type_id'), array($game_id, $_POST["game_type"]), "GAME_OF_TYPE"));
					} else
						$game_id = $_POST["game_id"];
					
					if ($_POST["game_editor_id"] == "NEW") {
						db_do_query(db_query_insert(array('editor_name', 'editor_link'), array(db_sqlize($_POST["editor_name"]), db_sqlize($_POST["editor_link"])), "GAME_EDITOR"));
						$editor_id = db_insert_id();
					} else
						$editor_id = $_POST["game_editor_id"];
					
					$sql = db_query_insert(array('item_id', 'game_id', 'game_editor_id', 'image_link', 'url', 'description', 'genre', 'rules', 'players', 'expansions', 'requirements', 'price', 'languages', 'did_you_know', 'forum_link'), array($item_id, $game_id, $editor_id, db_sqlize($filename), db_sqlize($_POST['url']), db_sqlize($_POST['description']), db_sqlize($_POST['genre']), db_sqlize($_POST['rules']), db_sqlize($_POST['players']), db_sqlize($_POST['expansions']), db_sqlize($_POST['requirements']), db_sqlize($_POST['price']), db_sqlize($_POST['languages']), db_sqlize($_POST['did_you_know']), $_POST['forum_link']), "GAME_FICHE");
					
					db_do_query(db_query_update(array('link'), array(db_sqlize("{$PAGE['dirpath']}/{$item_id}.fiche")), "GAME", "game_id = $game_id"));
					
					break;
				default:
					$sql = "";
					$inserted = 0;
					break;
			}
			if (!isset($inserted) and db_do_query($sql))
				$inserted = 1;
			
			// if problems arise, rollback
			if (!$inserted) // rollback !
				die("st went wrong!!");

			// subset insertion
			if ($_POST['sub_id'] == "NEW") {
				$sql = db_query_insert(array('sub_title'), array(db_sqlize($_POST['new_subset'])), "SUBSET");
				db_do_query($sql);
				$sub_id = db_insert_id();
				$sql = db_query_insert(array('dir_id', 'sub_id', 'sub_title'), array($THISDIR['dir_id'], $sub_id, db_sqlize($_POST['new_subset'])), "SUB_IN");
				db_do_query($sql);
			} else {
				$sub_id = $_POST['sub_id'];
			}

			// in_sub insertion
			$sql = db_query_insert(array('sub_id', 'item_id'), array($sub_id, $item_id), "ITEM_IN");
			db_do_query($sql);
			
			$new_url = $PAGE['dirpath'] . "/$item_id." . item_page_inc_type_string($_POST['item_type_id']);

			// process subscriptions
			$message = array(
				'alert' => "{$_SESSION['member']['nick']} plaatste een item in '<a href={$new_url}>{$THISDIR['title']}</a>' <span class=iteminfo>(je krijgt dit bericht omdat je je abonneerde op de directory '{$THISDIR['title']}'. Vanuit de directory zelf kan je je abonnement opzeggen)</span>",
				'subject' => "Nieuw item in '{$THISDIR['title']}'",
				'mail' => "{$_SESSION['member']['nick']} plaatste een nieuw item in '{$THISDIR['title']}': '<a href={$new_url}>{$_POST['title']}</a>'\n<quote author={$_SESSION['member']['nick']}>{$_POST['body']}</quote>\n<p>(je krijgt dit bericht omdat je je abonneerde op de directory '{$THISDIR['title']}'. Vanuit de directory zelf kan je je abonnement opzeggen)",
				'email' => ereg_replace("<quote.+</quote>", "", "{$_SESSION['member']['nick']} plaatste een nieuw item in '{$THISDIR['title']}': '{$_POST['title']}'\ndirectory: {$CONFIG['base_url']}{$PAGE['dirpath']}\nitem: {$CONFIG['base_url']}{$new_url}\n\n---------------------\n{$_POST['body']}\n---------------------\n\n(je krijgt dit bericht omdat je je abonneerde op de directory '{$THISDIR['title']}'. Vanuit de directory zelf kan je je abonnement opzeggen)"));
			handle_subscriptions("DIR", $THISDIR['dir_id'], $message);

			redirect($new_url);
			
		}
		
	}
		
	global $PAGE;
	$PAGE['content']['title'] = "Een item toevoegen";

	item_form();
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;
	print_form($PAGE['content']['form']);
	// what else to print ?
}

?>
